import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import { ApolloServer } from 'apollo-server-koa';
import { typeDefs } from './graphql/schema';
import { resolvers } from './graphql/resolvers';
import userRoutes from './routes/userRoutes';

const app = new Koa();
app.use(bodyParser());
app.use(userRoutes.routes());
app.use(userRoutes.allowedMethods());

const server = new ApolloServer({
  typeDefs,
  resolvers,
});

async function startServer() {
  await server.start();
  server.applyMiddleware({ app });

  const PORT = process.env.PORT || 4000;
  app.listen(PORT, () => {
    console.log(`Server ready at http://localhost:${PORT}${server.graphqlPath}`);
  });
}

startServer();